//
//  RBUtils.m
//  RBDemoProj
//
//  Created by Mac on 2020/10/14.
//  Copyright © 2020 SummerTea. All rights reserved.
//

#import "RBUtils.h"
//#import <AMapLocationKit/AMapLocationKit.h>
#import <Photos/Photos.h>
#import "RBAppDelegateHelper.h"

#define kRBLatKey @"kRBLatKey"
#define kRBLngKey @"kRBLngKey"
#define kRBPosTimeKey @"kRBPosTimeKey"

@interface RBUtils()<UIDocumentPickerDelegate>

@property(nonatomic, copy) void(^chooseDocumentBlock)(NSURL *url, NSData *data, NSString *fileName);

@end

@implementation RBUtils

+ (RBUtils *)share
{
    static id instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = RBUtils.new;
    });
    return instance;
}

+ (void)chooseDocumentWithCompletion:(void (^)(NSURL * _Nonnull, NSData * _Nonnull, NSString * _Nonnull))completion
{
    UIDocumentPickerViewController *vc = [UIDocumentPickerViewController.alloc initWithDocumentTypes:@[@"public.data",@"public.item"] inMode:UIDocumentPickerModeImport];
    vc.delegate = [RBUtils share];
    [RBUtils share].chooseDocumentBlock = completion;
    [RBAppDelegateHelper.keyWindow.rootViewController presentViewController:vc animated:YES completion:nil];
}

#pragma mark - UIDocumentPickerDelegate

- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray <NSURL *>*)urls
{
    if(urls.count && self.chooseDocumentBlock)
    {
        NSURL *url = urls.firstObject;
        NSData *data = [NSData dataWithContentsOfURL:url];
        NSString *fileName = @"document";
        NSArray *components = [url.path componentsSeparatedByString:@"/"];
        if(components.count)
        {
            fileName = components.lastObject;
        }
        self.chooseDocumentBlock(url, data, fileName);
    }
        
}


@end
